package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Remark;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.RemarkService;
import com.itwn.comic.vo.RemarkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * CRUD
 * @author cx
 * @date 2022/2/18
 * @apiNote
 */
@RestController
@RequestMapping("remark")
public class RemarkController {
// 评论的后台只要删除和搜索即可
    @Autowired
    RemarkService remarkService;


    /**
     * 删除评论(ok)
     * @param id
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> remove(@PathVariable("id") long id){
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.remarkService.removeByKey(id);
            result.setCode(200);
            result.setMsg("删除评论成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除评论失败!");
        }
        return result;
    }
    /**
     * 加精方法,前台调用修改也可,留白
     */
    /**
     * 添加评论
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Remark remark){
        //remark.setStatus("1"); 数据库给默认也可以
        remark.setRemarkTime(new Date());
        remark.setStatus(0);
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.remarkService.add(remark);
            result.setCode(200);
            result.setMsg("保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result;
    }

    /**
     * 修改评论
     */
    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Remark remark){

        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.remarkService.modify(remark);
            result.setCode(200);
            result.setMsg("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("编辑失败！");
        }
        return result;
    }

    @GetMapping("/setHot")
    public ResponseResult<Void> setHot(@RequestParam("id")Integer id,@RequestParam("status")Integer status){

        ResponseResult<Void> result = new ResponseResult<>();
        try{
            Remark rem = this.remarkService.queryRemarkById(id);
            if(status==1){
                status--;
                this.remarkService.modifyStatus(id,status);
            }else{
                status++;
                this.remarkService.modifyStatus(id,status);
            }
            result.setCode(200);
            result.setMsg("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("编辑失败！");
        }
        return result;
    }

    /**
     * 分页查询全部评论（只有二层楼）
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<RemarkVo>> listByPage(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                         @RequestParam(value="size",defaultValue = "2") Integer size,
                                                         @RequestParam(value="pid",defaultValue = "0") Integer parentId, String comicName, String username){
       ResponseResult<PageInfo<RemarkVo>> result = new ResponseResult<>();
       try{
           System.out.println(parentId+":"+comicName+":"+username);
           PageInfo<RemarkVo> pageInfo = this.remarkService.queryByPage(now,size,parentId,comicName,username);
           result.setCode(200);
           result.setMsg("查询评论成功!");
           result.setData(pageInfo);
       } catch (Exception e) {
           e.printStackTrace();
           result.setCode(201);
           result.setMsg("查询评论失败!");
       }
        return result;
    }
    /**
     * 分页查询精华评论(status=1),留白,前端给if也可
     */
}
